package com.xinxin.facelinker.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.Toast;

import com.easemob.chatuidemo.R;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.xinxin.facelinker.Config;
import com.xinxin.facelinker.activity.util.SliderBar;
import com.xinxin.facelinker.adapter.ShowPalsAdapter;
import com.xinxin.facelinker.domain.ShowPals;
import com.xinxin.facelinker.ui.button.views.ButtonFloat;
import com.xinxin.facelinker.ui.menu.ContextMenuDialogFragment;
import com.xinxin.facelinker.ui.menu.MenuObject;
import com.xinxin.facelinker.ui.menu.MenuParams;

import java.util.ArrayList;
import java.util.List;

public class ShowPalsFragment extends Fragment {

    private ListView lvShowPals;
    private String my_account_num;
    private String other_account_num;
    private HttpUtils httpUtils = new HttpUtils();
    private ArrayList<ShowPals> list = new ArrayList<ShowPals>();
    private SliderBar sliderBar;
    private SearchView searchView;
    private ButtonFloat btnMenu;
    private FragmentManager fragmentManager;
    private DialogFragment mMenuDialogFragment;
    private ShowPalsAdapter adapter;

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_show_pals, container, false);

        lvShowPals = (ListView) root.findViewById(R.id.lvShowPals);
        lvShowPals.setTextFilterEnabled(true);
        my_account_num = Config.getCachedAccountNum(getActivity());
        btnMenu = (ButtonFloat) root.findViewById(R.id.btnMenu);
        fragmentManager = getFragmentManager();

        initMenuFragment();

        btnMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMenuDialogFragment.onAttach(getActivity());
                fragmentManager.beginTransaction().add(mMenuDialogFragment, "").commit();
            }
        });

        searchView = (SearchView) root.findViewById(R.id.searchView);
        searchView.setIconifiedByDefault(false);
        searchView.setSubmitButtonEnabled(true);
        searchView.setQueryHint("查找");
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                //进行查找操作
                return false;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                //进行查找操作
                return true;
            }
        });

        RequestParams params = new RequestParams();
        params.addBodyParameter(Config.ACTION, Config.ACTION_SHOW_PALS);
        params.addBodyParameter(Config.KEY_MY_ACCOUNT_NUM, my_account_num);
        httpUtils.send(HttpRequest.HttpMethod.POST, Config.CATEGORIES_URL, params, new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        Gson gson = new Gson();
                        list = gson.fromJson(responseInfo.result, new TypeToken<List<ShowPals>>() {
                        }.getType());

                        adapter = new ShowPalsAdapter(getActivity(), list);
                        lvShowPals.setAdapter(adapter);

                        lvShowPals.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                other_account_num = list.get(position).getAccount_num();
                                Intent intent = new Intent(getActivity(), GetPalInfoActivity.class);
                                intent.putExtra(Config.KEY_OTHER_ACCOUNT_NUM, other_account_num);
                                startActivity(intent);
                            }
                        });
                    }

                    @Override
                    public void onFailure(HttpException e, String s) {
                        Toast.makeText(getActivity(), R.string.get_contacts_fail, Toast.LENGTH_SHORT).show();
                    }
                }
        );

        return root;
    }

    private void initMenuFragment() {
        MenuParams menuParams = new MenuParams();
        menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
        menuParams.setMenuObjects(getMenuObjects());
        menuParams.setClosableOutside(false);
        mMenuDialogFragment = ContextMenuDialogFragment.newInstance(menuParams);
    }

    private List<MenuObject> getMenuObjects() {

        List<MenuObject> menuObjects = new ArrayList<>();

        MenuObject close = new MenuObject();
        close.setResource(R.drawable.btn_close);

        MenuObject search = new MenuObject("搜索");
        Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.btn_search);
        search.setBitmap(b);

        menuObjects.add(close);
        menuObjects.add(search);

        return menuObjects;
    }

    public boolean onCreateOptionsMenu(final Menu menu) {
        MenuInflater inflater = getActivity().getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_image:
                if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
                    mMenuDialogFragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
                }
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onBackPressed() {
        if (mMenuDialogFragment != null && mMenuDialogFragment.isAdded()) {
            mMenuDialogFragment.dismiss();
        } else {
//            finish();
        }
    }
}
